import java.util.Arrays;

public class Q3 {

   public static int countPairs( int a[], int targetValue ){
       // Trivial case
       if (null == a || a.length <= 1) {
           return 0;
       }
       
       Arrays.sort(a);
       
       int count = 0;
       int head = 0, end = a.length - 1;
       while (head < end) {
           int sum = a[head] + a[end];
           if (sum > targetValue) {
               end--;
           } else if (sum < targetValue) {
               head++;
           } else {
               head++;
               end--;
               count++;
           }
       }
       return count;
   }
   
   public static void main(String[] args) {
	int n = 5;
	
	int targetValue = 4;
	int[] a = new int[n];
	for (int i = 0; i < n; i++) {
		a[i] = i;
	}
	int result = countPairs(a, targetValue);
	System.out.println(Arrays.toString(a));
	System.out.println(result);
}
}